package ishaportalserver.apps.insight.repository;

import static com.googlecode.objectify.ObjectifyService.ofy;
import ishaportalserver.apps.DataStoreNameSpaces;
import ishaportalserver.shared.apps.ErrorCodes;
import ishaportalserver.shared.apps.Response;
import ishaportalserver.shared.apps.insight.CenterProp;

import java.util.ArrayList;
import java.util.List;

import com.google.appengine.api.NamespaceManager;
import com.googlecode.objectify.ObjectifyService;

public class Repository implements IRepository {
	
	static {
		ObjectifyService.factory().register(MemberEntity.class);
		ObjectifyService.factory().register(CenterEntity.class);
	}

	@Override
	public List<MemberEntity> getMatches(CenterEntity c, String searchStr) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public MemberEntity saveMember(MemberEntity m) {
		
		NamespaceManager.set(DataStoreNameSpaces.INSIGHT);		
		ofy().save().entity(m).now();
		
		return m;
	}

	@Override
	public MemberEntity getMember(long memberId) {
		
		if (0 == memberId)
			return null;
		
		NamespaceManager.set(DataStoreNameSpaces.INSIGHT);
		
		MemberEntity me = ofy().load().type(MemberEntity.class).id(memberId).get();

		return me;
	}

	@Override
	public Response addCenter(String name) {
		
		Response resp = new Response();
								
		//name should not be already present
		long id = getCenterId(name);
		if (0 != id) {
			resp.status = ErrorCodes.Insight.CENTER_ALREADY_PRESENT;
			resp.message = "Center [" + name + "] is already present with id [" + id + "]";			
			return resp;			
		}
		
		CenterEntity entity = new CenterEntity();
		entity.name = name;
		
		NamespaceManager.set(DataStoreNameSpaces.INSIGHT);
		ofy().save().entity(entity).now();
		
		resp.status = ErrorCodes.SUCCESS;
		
		return resp;
	}

	@Override
	public List<CenterProp> getAllCenters() {
		NamespaceManager.set(DataStoreNameSpaces.INSIGHT);
		
		List<CenterEntity> entities = ofy().load().type(CenterEntity.class).list();
		
		List<CenterProp> props = new ArrayList<CenterProp>();
		
		for (CenterEntity e : entities) {
			props.add(e.toProp());
		}
		
		return props;
	}

	@Override
	public Response deleteCenter(long id) {
		NamespaceManager.set(DataStoreNameSpaces.INSIGHT);
		CenterEntity entity = ofy().load().type(CenterEntity.class).id(id).get();
		
		Response resp = new Response();
		if (null == entity) {
			resp.status = ErrorCodes.Insight.CENTER_ID_NOT_PRESENT;
			resp.message = "There is no center with id [" + id + "]";
			return resp;
		}
		
		ofy().delete().entity(entity).now();
		resp.status = ErrorCodes.SUCCESS;
		
		//TODO - check no existing members before deleting center
		
		return resp;
	}
	
	private long getCenterId(String name) {
		Repository r = new Repository();					
		List<CenterProp> all = r.getAllCenters();
		
		name = name.toLowerCase();
		long centerId = 0;
		for (CenterProp prop : all) {
			if (prop.name.toLowerCase().equals(name))
				centerId = prop.id;
		}
				
		return centerId;			
	}

	@Override
	public CenterProp getCenter(long centerId) {
		NamespaceManager.set(DataStoreNameSpaces.INSIGHT);
		
		CenterEntity entity = ofy().load().type(CenterEntity.class).id(centerId).get();
		
		CenterProp prop = null;
		if (null != entity) 
			prop = entity.toProp();
		
		return prop;
	}

	@Override
	public Response renameCenter(long id, String newName) {
		
		NamespaceManager.set(DataStoreNameSpaces.INSIGHT);
						
		CenterEntity entity = ofy().load().type(CenterEntity.class).id(id).get();
		
		Response resp = new Response();
		
		//check if id is valid
		if (null == entity) {			
			resp.status = ErrorCodes.Insight.CENTER_ID_NOT_PRESENT;
			resp.message = "There is no existing center with id [" + id + "]";
			return resp;
		}
		
		//if new name is different - check if it is valid
		if (! entity.name.toLowerCase().equals(newName.toLowerCase())) {
			long centerId = getCenterId(newName);
			if (0 != centerId) {				
				resp.status = ErrorCodes.Insight.CENTER_ALREADY_PRESENT;
				resp.message = "Center [" + newName + "] is already present with id [" + centerId + "]";			
				return resp;			
			}
		}
		
		//save with new name
		entity.name = newName;
		ofy().save().entity(entity).now();
		resp.status = ErrorCodes.SUCCESS;
		
		return resp;		
	}
}
